package SetMap;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @TODO
 * @Author xu
 * 2025/3/17
 */
public class DouDiZhuGame {
    private static final String[] SUITS = {"♠", "♥", "♣", "♦"};
    private static final String[] RANKS = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2", "大王", "小王"};

    public static void main(String[] args) {
        List<Card> deck = createDeck();
        Collections.shuffle(deck); // Step 2: Shuffle the deck

        // Step 3: Deal cards
        List<List<Card>> players = dealCards(deck);

        // Step 4: Determine bottom cards (底牌)
        List<Card> bottomCards = new ArrayList<>(deck.subList(51, 54));

        // Step 5: Sort players' hands
        sortPlayersHands(players);

        // Output the results
        displayResults(players, bottomCards);
    }

    private static List<Card> createDeck() {
        List<Card> deck = new ArrayList<>();
        for (int i = 0; i < SUITS.length; i++) {
            for (int j = 0; j < RANKS.length - 2; j++) { // Exclude the two kings for now
                deck.add(new Card(SUITS[i], RANKS[j], deck.size())); // (suit, rank, index)
            }
        }
        // Add the two jokers
        deck.add(new Card("", "大王", deck.size())); // Big joker
        deck.add(new Card("", "小王", deck.size())); // Small joker
        return deck;
    }

    private static List<List<Card>> dealCards(List<Card> deck) {
        List<List<Card>> players = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            players.add(new ArrayList<>());
        }

        for (int i = 0; i < 51; i++) {
            players.get(i % 3).add(deck.get(i)); // Distribute 51 cards among 3 players
        }
        return players;
    }

    private static void sortPlayersHands(List<List<Card>> players) {
        for (List<Card> hand : players) {
            hand.sort((c1, c2) -> {
                int rankComparison = getRankValue(c1.rank) - getRankValue(c2.rank);
                if (rankComparison == 0) {
                    return c1.index - c2.index; // If ranks are the same, sort by index
                }
                return rankComparison;
            });
        }
    }

    private static int getRankValue(String rank) {
        switch (rank) {
            // Numeric card values
            case "3": return 0;
            case "4": return 1;
            case "5": return 2;
            case "6": return 3;
            case "7": return 4;
            case "8": return 5;
            case "9": return 6;
            case "10": return 7;
            case "J": return 8;
            case "Q": return 9;
            case "K": return 10;
            case "A": return 11;
            case "2": return 12;
            case "小王": return 13; // Small joker
            case "大王": return 14; // Big joker
            default: return -1; // Invalid rank
        }
    }

    private static void displayResults(List<List<Card>> players, List<Card> bottomCards) {
        for (int i = 0; i < players.size(); i++) {
            System.out.println("Player " + (i + 1) + " cards: " + players.get(i));
        }
        System.out.println("Bottom cards: " + bottomCards);
    }
}
